package com.terra;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

public class Rose extends ImageView {
	
	float fBearing = 0;
	
	public Rose(Context context, AttributeSet attrs) {
		super(context, attrs);
		//Log.v("Debug", "Creating Rose");
		
		//ImageView myRose = (ImageView) findViewById(R.id.imageCompassRose);
		setImageResource(R.drawable.compassrose);
		Log.v("Debug", "Rose created");
		
	}

	public void SetBearing(double dHeading) {
		fBearing = (float) dHeading;
	}
	
	public void update() {
		invalidate();
	}
	
	public void onDraw(Canvas canvas) {

		Globals.dHeading = Globals.myLocationOverlay.getOrientation();

    	Bitmap _scratch = BitmapFactory.decodeResource(getResources(), R.drawable.compassrose);
    	canvas.drawColor(Color.WHITE);
    	Matrix matrix = new Matrix();

    	matrix.setRotate((float) -Globals.dHeading, _scratch.getWidth()/2, _scratch.getHeight()/2);
    	canvas.drawBitmap(_scratch, matrix, null);
    	
    	Bitmap _scratch2 = BitmapFactory.decodeResource(getResources(), R.drawable.compassarrow);

    	matrix.setRotate((float) (fBearing - Globals.dHeading), _scratch2.getWidth()/2, _scratch2.getHeight());
    	matrix.postTranslate(_scratch.getWidth()/2-_scratch2.getWidth()/2, _scratch.getHeight()/2-_scratch2.getHeight());
    	Paint paint = new Paint();
    	paint.setAlpha(225);

    	canvas.drawBitmap(_scratch2, matrix, paint);
    }
}
